package xin.jishu.ai.providers;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.common.Identifier;
import org.openstack4j.model.identity.v3.Project;
import org.openstack4j.model.identity.v3.User;
import org.openstack4j.openstack.OSFactory;
import xin.jishu.ai.entities.Student;

/**
 * @author sxsx欧克 <wo@jishu.xin>
 */
public class KeyStone extends BaseProvider {

    static {
        OSFactory.enableHttpLoggingFilter(false);
    }

    public static OSClient.OSClientV3 client = null;
    public static final Identifier DOMAIN = Identifier.byId("default");
    public static final Identifier PROJECT = Identifier.byId("f15318f55892474888b18a452e6e51a5");
    public static final Identifier TARGET_DOMAIN = Identifier.byId("b4f5e94899734ce2809f8666553a0b30");
    public static final Identifier ROLE_MEMBER_ID = Identifier.byId("8b8d08ab56074b7496f445e934284008");

    @Override
    public void initialize() {
        KeyStone.client = OSFactory.builderV3()
                .endpoint("http://172.18.91.1:5000/v3")
                .credentials("admin", "oRZNOc0eAqiaMwmJiIS15mSrbWo2fUvtGHEzP942", KeyStone.DOMAIN)
                .scopeToProject(KeyStone.PROJECT, KeyStone.DOMAIN)
                .authenticate();
    }

    @Override
    public boolean exists(Student student) {
        return KeyStone.client.identity()
                .users()
                .getByName(
                        String.valueOf(
                                student.getId()
                        ),
                        KeyStone.TARGET_DOMAIN.getId()
                ) != null;
    }

    public static Project getOrCreateProject(Student student) {
        Project project = KeyStone.client.identity()
                .projects()
                .getByName(
                        String.valueOf(
                                student.getId()
                        ),
                        KeyStone.TARGET_DOMAIN.getId()
                );

        if (project == null) {
            project = Builders.project()
                    .domainId(
                            KeyStone.TARGET_DOMAIN.getId()
                    )
                    .name(
                            String.valueOf(
                                    student.getId()
                            )
                    )
                    .enabled(true)
                    .build();

            project = KeyStone.client.identity()
                    .projects()
                    .create(project);
        }

        return project;
    }

    @Override
    public void create(Student student) {
        Project project = KeyStone.getOrCreateProject(student);

        User user = Builders.user()
                .name(
                        String.valueOf(
                                student.getId()
                        )
                )
                .domainId(
                        KeyStone.TARGET_DOMAIN.getId()
                )
                .defaultProjectId(
                        project.getId()
                )
                .description(
                        student.getClassName()
                )
                .password(
                        String.valueOf(
                                student.getPhone()
                        )
                )
                .build();

        user = KeyStone.client.identity()
                .users()
                .create(user);
        // 将 用户 写入 项目 (admin)
        KeyStone.client.identity()
                .roles()
                .grantProjectUserRole(
                        project.getId(), user.getId(),
                        KeyStone.ROLE_MEMBER_ID.getId()
                );
    }
}
